package fsesl

import (
	"callsips/internal/model/sips/fslog"
	"callsips/internal/svc"
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zusrut/fsock"
	"strings"
)

type ChannelEventHandler struct {
	svcCtx *svc.ServiceContext
}

// 通道创建
func (c *ChannelEventHandler) ChannelCreateHandler(event string, id int) {
	var filterHeaders []string
	eventMap := fsock.FSEventStrToMap(event, filterHeaders)
	c.paserChannel(eventMap, "新通道创建")
}

// 被叫方收到呼叫请求（如回铃音）
func (c *ChannelEventHandler) ChannelProgressHandler(event string, id int) {
	var filterHeaders []string
	eventMap := fsock.FSEventStrToMap(event, filterHeaders)
	c.paserChannel(eventMap, "呼叫进展")
}

// 被叫方接听通话
func (c *ChannelEventHandler) ChannelAnswerHandler(event string, id int) {
	var filterHeaders []string
	eventMap := fsock.FSEventStrToMap(event, filterHeaders)
	c.paserChannel(eventMap, "通道应答")
}

// 两条通话通道（A/B腿）桥接成功
func (c *ChannelEventHandler) ChannelBridgeHandler(event string, id int) {
	var filterHeaders []string
	eventMap := fsock.FSEventStrToMap(event, filterHeaders)
	c.paserChannel(eventMap, "通道桥接")
}

// 通话挂断请求发起
func (c *ChannelEventHandler) ChannelHangupHandler(event string, id int) {
	var filterHeaders []string
	eventMap := fsock.FSEventStrToMap(event, filterHeaders)
	c.paserChannel(eventMap, "通道挂断")
}

// 通话完全结束（资源释放）
func (c *ChannelEventHandler) ChannelHangupCompleteHandler(event string, id int) {
	var filterHeaders []string
	eventMap := fsock.FSEventStrToMap(event, filterHeaders)
	c.paserChannel(eventMap, "挂断完成")
}

// 通道资源销毁
func (c *ChannelEventHandler) ChannelDestroyHandler(event string, id int) {
	var filterHeaders []string
	eventMap := fsock.FSEventStrToMap(event, filterHeaders)
	c.paserChannel(eventMap, "通道销毁")
}

func (c *ChannelEventHandler) paserChannel(resEventMap map[string]string, name string) {
	var fslogInfo fslog.SipsFslog = fslog.SipsFslog{
		CoreUuid:                resEventMap["Channel-Call-UUID"],
		EventName:               resEventMap["Event-Name"],
		EventNameCn:             name,
		EventDateLocal:          resEventMap["Event-Date-Local"],
		ChannelCallState:        resEventMap["Channel-Call-State"],
		ChannelState:            resEventMap["Channel-State"],
		CallerNetworkAddr:       resEventMap["Caller-Network-Addr"],
		EventSequence:           resEventMap["Event-Sequence"],
		CallDirection:           resEventMap["Call-Direction"],
		CallerUsername:          resEventMap["Caller-Username"],
		CallerCallerIdName:      resEventMap["Caller-Caller-ID-Name"],
		CallerDestinationNumber: resEventMap["Caller-Destination-Number"],
		CallerUniqueId:          resEventMap["Caller-Unique-ID"],
		CallerSource:            resEventMap["Caller-Source"],
		CallerContext:           resEventMap["Caller-Context"],
		ChannelReadCodecName:    resEventMap["Channel-Read-Codec-Name"],
		ChannelReadCodecRate:    resEventMap["Channel-Read-Codec-Rate"],
		ChannelWriteCodecName:   resEventMap["Channel-Write-Codec-Name"],
		ChannelWriteCodecRate:   resEventMap["Channel-Write-Codec-Rate"],
		ChannelName:             strings.ReplaceAll(resEventMap["Channel-Name"], "%40", "@"),
		VariableSipCallId:       strings.ReplaceAll(resEventMap["variable_sip_call_id"], "-", ""),
	}

	if _, err := c.svcCtx.SipFsLog.Insert(context.Background(), &fslogInfo); err != nil {
		logx.Infof("通道信息：%s,%s,%s,%s", name, fslogInfo.CoreUuid, fslogInfo.EventNameCn, fslogInfo.EventName, err.Error())
	}
}

func NewChannelEventHandler(svc *svc.ServiceContext) *ChannelEventHandler {
	return &ChannelEventHandler{
		svcCtx: svc,
	}
}
